package com.ouheng.wxgz;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ouheng.shop.domain.ProductDO;


public class WxGzpush {

	public static String getToken(){
		Map<String,Object> map=new HashMap<>();
	        CloseableHttpClient httpclient = HttpClients.createDefault(); 
	        String RESTFUL_BASE_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx06da6ee3fb929221&secret="+"3439afb58720e66351108cdffa08aaed";
	        HttpPost httppost = new HttpPost(RESTFUL_BASE_URL); 
	        // 创建参数队列    
	        List<NameValuePair> formparams = new ArrayList< >(); 
	        Set<String> set = map.keySet();
	        Iterator<String> it = set.iterator();
	        while(it.hasNext())
	        {
	            String key = it.next();
	            formparams.add(new BasicNameValuePair(key, (String) map.get(key)));
	        }
	        
	        UrlEncodedFormEntity uefEntity = null;
	        try {  
	            uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");  
	            httppost.setEntity(uefEntity);  
	            CloseableHttpResponse response = httpclient.execute(httppost);  
	            try {
	                HttpEntity entity = response.getEntity();  
	                if (entity != null) {  
	                    String jsonStr = EntityUtils.toString(entity, "UTF-8");
	                    
	                	String access_token=JSON.parseObject(jsonStr).getString("access_token");
	                    
	                    return access_token;
	                }  
	            } finally {  
	                response.close();  
	            }  
	        } catch (ClientProtocolException e) {  
	            e.printStackTrace();  
	        } catch (UnsupportedEncodingException e1) {  
	            e1.printStackTrace();  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        }catch(Exception e){
	        	e.printStackTrace();
	        } finally {  
	            // 关闭连接,释放资源    
	            try {  
	                httpclient.close();  
	            } catch (IOException e) {  
	                e.printStackTrace();  
	            }  
	        }  
	        return null;
	  }
	  
	
	public static JSONObject push(Map<String,Object> map){
		try {
			String access_token=getToken();
			//https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN
			String host=	"https://api.weixin.qq.com";
			 String url="/cgi-bin/message/template/send?access_token="+access_token;
			 Map<String,String> headers=new HashMap<String, String>();
			 Map<String,String> querys=new HashMap<String, String>();
			 HttpResponse httpResponse=HttpUtils.doPost(host, url, "POST", headers, querys, JSON.toJSONString(map));
			 HttpEntity entity = httpResponse.getEntity();  
		     if (entity != null) {  
		         String jsonStr = EntityUtils.toString(entity, "UTF-8");
		        System.out.println(jsonStr);
		     }  
			
		} catch (Exception e) {
			
		}
		
		return null;
	}
	
	public static JSONObject push(Map<String,Object> map,String access_token){
		try {
			//https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN
			String host=	"https://api.weixin.qq.com";
			 String url="/cgi-bin/message/template/send?access_token="+access_token;
			 Map<String,String> headers=new HashMap<String, String>();
			 Map<String,String> querys=new HashMap<String, String>();
			 HttpResponse httpResponse=HttpUtils.doPost(host, url, "POST", headers, querys, JSON.toJSONString(map));
			 HttpEntity entity = httpResponse.getEntity();  
		     if (entity != null) {  
		         String jsonStr = EntityUtils.toString(entity, "UTF-8");
		        System.out.println(jsonStr);
		     }  
			
		} catch (Exception e) {
		}
		return null;
	}
	
	
	  
public static void main1(String[] args) {
	try {
		Map<String,Object> first=new HashMap<String, Object>();
		first.put("value", "您的订单已发货，邮差正快马加鞭速递中:");
		first.put("color", "#173177");
		
		Map<String,Object> keyword1=new HashMap<String, Object>();
		keyword1.put("value", "hello");
		keyword1.put("color", "#173177");
		
		
		Map<String,Object> keyword2=new HashMap<String, Object>();
		keyword2.put("value", "李文东");
		keyword2.put("color", "#173177");
		
		
		Map<String,Object> keyword3=new HashMap<String, Object>();
		keyword3.put("value", "我感觉不行！");
		keyword3.put("color", "#173177");
		

		Map<String,Object> keyword4=new HashMap<String, Object>();
		keyword4.put("value", "我感觉不行！");
		keyword4.put("color", "#173177");
		

		Map<String,Object> remark=new HashMap<String, Object>();
		remark.put("value", "我感觉不行！");
		remark.put("color", "#173177");
		
		Map<String,Object> data=new HashMap<String, Object>();
		
		data.put("first", first);
		data.put("keyword1", keyword1);
		data.put("keyword2", keyword2);
		data.put("keyword3", keyword3);
		data.put("keyword4", keyword4);
		data.put("remark", remark);
		
		Map<String,Object> m=new HashMap<String, Object>();
		m.put("data", data);
		
		m.put("touser", "oEjF91o4Na_FAxSeQtBnVyrE3T_4");
		
		m.put("template_id", "k7mdD-WEeKnries_uirJ3qBhagXbchuxc9WSXXyj2WQ");
		
		
		WxGzpush.push(m);
	
	} catch (Exception e) {
	}
}


public static void send(String touser, Map<String,Object> map){
	try {
		Map<String,Object> first=new HashMap<String, Object>();
		first.put("value", map.get("first"));
		first.put("color", "#173177");
		
		Map<String,Object> keyword1=new HashMap<String, Object>();
		keyword1.put("value", map.get("name"));
		keyword1.put("color", "#173177");
		
		Map<String,Object> keyword2=new HashMap<String, Object>();
		keyword2.put("value", map.get("createDate"));
		keyword2.put("color", "#173177");
		
		Map<String,Object> remark=new HashMap<String, Object>();
		remark.put("value", map.get("remark"));
		remark.put("color", "#173177");
		
		Map<String,Object> data=new HashMap<String, Object>();
		
		data.put("first", first);
		data.put("keyword1", keyword1);
		data.put("keyword2", keyword2);
		data.put("remark", remark);
		
		Map<String,Object> m=new HashMap<String, Object>();
		m.put("data", data);
		m.put("touser", touser);
		m.put("template_id", "AcZ56uYClIvRdPJQr4Zgh2_ETMu4blMK5tsUNDv2Rdo");
		Map<String, Object> miniprogram=new HashMap<>();
		miniprogram.put("appid", "wxb7eef9068526ea2f");
		miniprogram.put("pagepath", "pages/home/index/index?pageId="+map.get("id"));
		m.put("miniprogram", miniprogram);
		WxGzpush.push(m);
		
	} catch (Exception e) {
		
	}
}

public static void main(String[] args) {
	try {
	
		Map<String,Object> first=new HashMap<String, Object>();
		first.put("value", "您的订单已发货，邮差正快马加鞭速递中:");
		first.put("color", "#173177");
		
		Map<String,Object> keyword1=new HashMap<String, Object>();
		keyword1.put("value", "hello");
		keyword1.put("color", "#173177");
		
		
		Map<String,Object> keyword2=new HashMap<String, Object>();
		keyword2.put("value", "李文东");
		keyword2.put("color", "#173177");
		

		

		Map<String,Object> remark=new HashMap<String, Object>();
		remark.put("value", "我感觉不行！");
		remark.put("color", "#173177");
		
		Map<String,Object> data=new HashMap<String, Object>();
		
		data.put("first", first);
		data.put("keyword1", keyword1);
		data.put("keyword2", keyword2);
	
		data.put("remark", remark);
		
		Map<String,Object> m=new HashMap<String, Object>();
		m.put("data", data);
		
		m.put("touser", "oEjF91o4Na_FAxSeQtBnVyrE3T_4");
		
		m.put("template_id", "AcZ56uYClIvRdPJQr4Zgh2_ETMu4blMK5tsUNDv2Rdo");
		
		
		WxGzpush.push(m);
	
	} catch (Exception e) {
		// TODO: handle exception
	}









}

}
